﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JuCloudy.Sys.App.Category
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            
            this.Loaded += (obj, e) =>
            {
                InitData();
                OnLoad();
            };
        }

        private JuCloudy.Framework.Interface.IStructure Structure =
                   (JuCloudy.Framework.Interface.IStructure)Application.Current.RootVisual;

        private ObservableCollection<JuCloudy.Data.Entity.SysCategory> entities
        {
            get { return new ObservableCollection<Data.Entity.SysCategory>((IEnumerable<Data.Entity.SysCategory>)this._dg.ItemsSource); }
            set { this._dg.ItemsSource = value; }
        }

        private ObservableCollection<JuCloudy.Data.Entity.SysStructure> SysStructures { get; set; }

        private void InitData()
        {
            Structure.ShowLoading();
            SystemService.WSSysServiceSoapClient client
                = new SystemService.WSSysServiceSoapClient();
            client.GetCategoryEntitiesAsync();
            client.GetCategoryEntitiesCompleted += (obj, ex) =>
            {
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysCategory>))
                    as ObservableCollection<JuCloudy.Data.Entity.SysCategory>;

                client.GetStructureEntitiesAsync();
                client.GetStructureEntitiesCompleted += (objx, exx) =>
                {
                    Structure.CloseLoading();
                    if (exx.Error != null)
                        throw exx.Error;

                    this.SysStructures = exx.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysStructure>))
                        as ObservableCollection<JuCloudy.Data.Entity.SysStructure>;
                };
            };
        }

        public void insert(JuCloudy.Data.Entity.SysCategory entity)
        {
            Structure.ShowLoading();
            SystemService.WSSysServiceSoapClient client
                = new SystemService.WSSysServiceSoapClient();
            client.InsertCategoryAsync(entity.Serialize());
            client.InsertCategoryCompleted += (obj, ex) =>
            {
                Structure.CloseLoading();
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysCategory>))
                    as ObservableCollection<JuCloudy.Data.Entity.SysCategory>;
                Structure.CloseChildDialog();
            };
        }

        public void delete(JuCloudy.Data.Entity.SysCategory entity)
        {
            Structure.ShowLoading();
            SystemService.WSSysServiceSoapClient client
                = new SystemService.WSSysServiceSoapClient();
            client.DeleteCategoryAsync(entity.Serialize());
            client.DeleteCategoryCompleted += (obj, ex) =>
            {
                Structure.CloseLoading();
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysCategory>))
                    as ObservableCollection<JuCloudy.Data.Entity.SysCategory>;
                Structure.CloseChildDialog();
            };
        }

        private void OnLoad()
        {
            this._insertbtn.Click += (obj, e) =>
            {
                var dialog = new controls.insert();
                dialog.Mainpage = this;
                dialog.SysStructures = this.SysStructures;
                Structure.ShowChildDialog(dialog, "新增分类");
            };

            this._updatabtn.Click += (obj, e) =>
            {
                var dialog = new controls.insert();
                dialog.Mainpage = this;
                dialog.SysStructures = this.SysStructures;
                dialog.Entity = this._dg.SelectedItem as JuCloudy.Data.Entity.SysCategory;
                Structure.ShowChildDialog(dialog, "编辑分类");
            };
        }

        private void _dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                this._updatabtn.IsEnabled = true;
                this._deletebtn.IsEnabled = true; 
            }
            else
            {
                this._updatabtn.IsEnabled = false;
                this._deletebtn.IsEnabled = false; 
            }
        }
    }
}
